#include "test_gnss.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "string.h"
#include "stdlib.h"

static void cm_test_gnss_cb(uint32_t type, const char *nmea, uint32_t len)
{
    //cm_demo_printf("type:%d\n, nmea:%s\n, len:%d\n",type,nmea,len);
    cm_demo_printf("%s\n",nmea);
}

static void cm_test_send_data_cb(const char *data, uint32_t len)
{
    cm_demo_printf("data_cb:%s\n, len_cb:%d\n",data,len);
}

static void cm_test_agnss_data_callback(cm_agnss_update_result_e mode, const char *update_time, uint32_t size)
{
    cm_demo_printf("agnss_mode:%d, update_time:%s, data_size:%d\n",mode,update_time,size);
}

void cm_test_gnss_operation(unsigned char *cmd[],int len)
{
    if(0 == strcmp("OPEN",cmd[2]))
    {
        cm_gnss_type_e gnss_type = (cm_gnss_type_e)atoi(cmd[3]);//1-6
        cm_agnss_enable_e agnss_enable = (cm_agnss_enable_e)atoi(cmd[4]);//1，2
        cm_demo_printf("gnss cm_gnss_open ret:%d\n", cm_gnss_open( gnss_type,  agnss_enable));

    }else if (0 == strcmp("CLOSE",cmd[2]))
    {
        cm_gnss_close();
    }else if (0 == strcmp("CFG",cmd[2]))
    {
        cm_gnss_config_type_e config_type = (cm_gnss_config_type_e)atoi(cmd[3]);
        uint32_t *value;
        unsigned char *value_2;
        switch(config_type)
        {
            case 1:
            {
                uint32_t type_value = strtoul(cmd[4],(void**)0,16);
                //value = &type_value;
                cm_demo_printf("cmd[4]:%s\n",cmd[4]);
                cm_demo_printf("value:%u\n",type_value);
                cm_demo_printf("gnss cm_gnss_config 1 ret:%d\n",cm_gnss_config(config_type, &type_value));
                break;
            }
            case 2:
            {
                uint32_t type_value = strtoul(cmd[4],(void**)0,10);
                //value = &type_value;
                cm_demo_printf("value:%u\n",type_value);
                cm_demo_printf("gnss cm_gnss_config 2 ret:%d\n",cm_gnss_config(config_type, &type_value));
                break;
            }
            case 3:
            {
                value_2 = cmd[4];
                cm_demo_printf("value:%s\n",*value_2);
                cm_demo_printf("gnss cm_gnss_config 3 ret:%d\n",cm_gnss_config(config_type, value));
                break;
            }
            case 4:
            {

                cm_agnss_location_t type_value = {
                    .latitude = strtof(cmd[4],NULL),
                    .longitude = strtof(cmd[5],NULL),
                    .mode = atoi(cmd[6]),
                };
                cm_demo_printf("gnss cm_gnss_config 4 ret:%d\n",cm_gnss_config(config_type, &type_value));
                cm_demo_printf("latitude:%f, longitude:%f, mode:%d\n",type_value.latitude,type_value.longitude,type_value.mode);
                break;
            }
            case 5:
            {
                cm_demo_printf("param not support now!\n");
                break;
            }
            case 6:
            {
                cm_demo_printf("param not support now!\n");
                break;
            }
            default :
            {
                cm_demo_printf("param error!\n");
                return;
            }
        }
        
    }else if (0 == strcmp("SET_CB",cmd[2]))
    {
        uint32_t nmea_type = strtoul(cmd[3],(void**)0,16);//1,2,4,8,16,32,64,128,256,512,1024,2048,4096
        cm_gnss_nmea_callback callback = cm_test_gnss_cb;
        cm_demo_printf("gnss set_nmea_callback ret:%d\n", cm_gnss_set_nmea_callback(callback, nmea_type));

    }else if (0 == strcmp("RESET",cmd[2]))
    {
        cm_demo_printf("gnss cm_gnss_reset ret:%d\n",cm_gnss_reset());

    }else if (0 == strcmp("SEND_CMD",cmd[2]))
    {
        const char *raw_data = cm_malloc(20);
        memset(raw_data,0,20);
        strcpy(raw_data,cmd[3]);
        uint32_t len = strlen(raw_data);
        cm_demo_printf("data:%s, len:%d\n",raw_data,len);
        cm_gnss_rawdata_rsp_callback callback = cm_test_send_data_cb;
        //cm_demo_printf("gnss cm_gnss_send_raw_data ret:%d\n", cm_gnss_send_raw_data(raw_data,len,callback));
        osDelay(100);
        cm_free(raw_data);
        cm_demo_printf("gnss cm_gnss_send_raw_data ret:%d\n", cm_gnss_send_raw_data("RESET,4",7, callback));
    }else if (0 == strcmp("GET_LOC",cmd[2]))
    {
        cm_gnss_location_info_t info = {0};
        cm_demo_printf("gnss cm_gnss_get_location_info ret:%d\n",cm_gnss_get_location_info(&info));
        // cm_demo_printf("GNSS  info->date_time.tm_sec=%d\n", info.date_time.tm_sec);
        // cm_demo_printf("GNSS  info->date_time.tm_min=%d\n", info.date_time.tm_min);
        // cm_demo_printf("GNSS  info->date_time.tm_hour=%d\n", info.date_time.tm_hour);
        // cm_demo_printf("GNSS  info->date_time.tm_mday=%d\n", info.date_time.tm_mday);
        // cm_demo_printf("GNSS  info->date_time.tm_mon=%d\n", info.date_time.tm_mon);
        // cm_demo_printf("GNSS  info->date_time.tm_year=%d\n", info.date_time.tm_year);
        // cm_demo_printf("GNSS  info->date_time.tm_wday=%d\n", info.date_time.tm_wday);
        // cm_demo_printf("GNSS  info->date_time.tm_yday=%d\n", info.date_time.tm_yday);
        // cm_demo_printf("GNSS  info->date_time.tm_isdst=%d\n", info.date_time.tm_isdst);
        cm_demo_printf("GNSS  info->latitude=%lf\n", info.latitude);
        cm_demo_printf("GNSS  info->longitude=%lf\n", info.longitude);
        cm_demo_printf("GNSS  info->hdop=%lf\n", info.hdop);
        cm_demo_printf("GNSS  info->altitude=%lf\n", info.altitude);
        cm_demo_printf("GNSS  info->fix=%d\n", info.fix);
        cm_demo_printf("GNSS  info->cog=%f\n", info.cog);
        cm_demo_printf("GNSS  info->spkm=%f\n", info.spkm);
        cm_demo_printf("GNSS  info->spkn=%d\n", info.spkn);
        cm_demo_printf("GNSS  info->nsat=%hu\n", info.nsat);
        cm_demo_printf("GNSS  info->dtype=%hu\n", info.dtype);
    }else if (0 == strcmp("GET_NMEA",cmd[2]))
    {

        cm_demo_printf("gnss cm_gnss_req_nmea ret:%d\n", cm_gnss_req_nmea());
    }
    else if(0 == strcmp("AGNSS_STATE",cmd[2]))
    {
        cm_agnss_update_result_e agnss_mode = -1;
//        cm_demo_printf("cm_agnss_update_result_e:%d, agnss_mode:%d, int:%d\n",sizeof(cm_agnss_update_result_e),sizeof(agnss_mode),sizeof(int));
        char update_time[15] = {0};
        uint32_t data_size = 0;
        int agnss_state_query = cm_agnss_state_query(&agnss_mode,update_time,sizeof(update_time),&data_size);
        cm_demo_printf("cm_agnss_state_query ret:%d\n",agnss_state_query);
        cm_demo_printf("agnss_mode:%d, update_time:%s, data_size:%u\n",agnss_mode,update_time,data_size);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,agnss_state_query,cm_agnss_state_query);
    }
    else if(0 == strcmp("AGNSS_UPDATE",cmd[2]))
    {
    
        int32_t agnss_data_start_update = cm_agnss_data_start_update(cm_test_agnss_data_callback);
        cm_demo_printf("cm_agnss_data_start_update ret:%d\n",agnss_data_start_update);
         OCTEST_FUNC_EXPECT_EQUAL_INT32(0,agnss_data_start_update,cm_agnss_data_start_update);
    }
    else if(0 == strcmp("DGNSS_ENABLE",cmd[2]))
    {
        /**
         *  @brief dgnss 功能打开
         *  
         *  @param [in] void
         *  
         *  @return  
         *    = 0  - 成功 \n
         *    = -1 - 失败
         *
         *  @details 不支持。
         *  @details 功能开启后，可以从NMEA/GGA数据中判断UE是否处于差分定位模式中。
         */
        //int32_t cm_dgnss_enable(void);
    }else if(0 == strcmp("DGNSS_DISABLE",cmd[2]))
    {
        /**
         *  @brief dgnss 功能关闭
         *  
         *  @param [in] void
         *  
         *  @return  
         *    = 0  - 成功 \n
         *    = -1 - 失败
        *
        *  @details 不支持。
        */
        //int32_t cm_dgnss_disable(void);
    }
    else
    {
        cm_demo_printf("param error!\n");
    }
}
